package com.application.meizhi.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/18 0018.
 * adapter基础类，添加对数据的各种方法
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter {

    protected List<T> mData;
    protected Context context;

    protected OnItemClickListener mOnItemClickListener;

    public BaseRecyclerAdapter(Context context) {
        this.context = context;
        this.mData = new ArrayList<>();
    }

    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public void addDataSetToStart(List<T> list) {
        mData.addAll(0, list);
        notifyDataSetChanged();
    }

    public void addDataSetToEnd(List<T> list) {
        mData.addAll(list);
        notifyDataSetChanged();
    }

    public void resetDataSet(List<T> list) {
        mData.clear();
        mData.addAll(list);
        notifyDataSetChanged();
    }

    public List<T> getDataSet() {
        return mData;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    protected void setupItemClickListener(ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(position);
            } else {
                Logger.d("BaseRecyclerAdapter", "mOnItemClickListener为空！");
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}